package com.iMusic.integrationLayer;

import java.util.List;
import java.util.Vector;

import com.iMusic.util.Parameters;

/*
 * Manager DBConnections (Singleton)
 */
public class DBConnectionManager {

	static private DBConnectionManager dbConnMgr = null;
	
	Vector<DBConnection> dbConnList = null;
	
	
	/*
	 * Constructor
	 */
	DBConnectionManager() {
		
	}
	
	/*
	 * Gets DBConnectionManager
	 */
	static public DBConnectionManager getDBConnectionManager() {
		
		if (dbConnMgr == null) {
			dbConnMgr = new DBConnectionManager();
		}
		
		return dbConnMgr;
	}
	
	
	/*
	 * Creates DBConnection
	 */
	public DBConnection getDBConnection() {
		
		DBConnection dbConn = null;
		
		if (dbConnList == null) {
			dbConnList = new Vector<DBConnection>();
		}
		
		if (dbConnList.size() < Parameters.DB_NBR_CONNECTIONS) {
			dbConn = new DBConnection();
			dbConnList.add(dbConn);
		}
		
		return dbConn;
	}

	
	/*
	 * Frees a DBConnection 
	 */
	public boolean freeDBConnection(DBConnection dbConn) {
		
		boolean ret = false;
		
		if (dbConnList.contains(dbConn)) {
			
			dbConnList.remove(dbConn);
			dbConn = null;
			
			ret = true;
		}
		
		return ret;
	}
	
	
	
}
